/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#include "../import/head.h"
#include "../data_struct/head.h"
#include "../impl/head.h"
#include "log_msg.h"

///////////////////////////////////////////////////////////////////////////////////////////////////

namespace xos_log
{
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    static log_msg::ITEM_DATA xos_item_data;

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    log_msg::log_msg()
    {
        init_data();
    }

    log_msg::~log_msg()
    {
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////
    // 帮助函数

    int log_msg::get_xos_item_data( ITEM_DATA*& item_ptr )
    {
        int ret = 0;
        xos_item_data.set_container( mgr::container() );
        item_ptr = &xos_item_data;
        return ret;
    }

    int log_msg::fmt_log()
    {
        int ret = 0;
        {
            m_fFmt( mgr::container(), this, m_pString );
            m_log = m_pString->get(0,0);
            fmt();
        }
        return ret;
    }

    int log_msg::fmt()
    {
        int ret = 0;

        xos_box::i_string * pStr = m_pString;
        xos::i_time * pTime = m_pTime;

        if( 0 == ret )
        {
            m_nMilliSecond = pTime->get_milli_second();
            m_nMinute = pTime->get_minute();
            m_nSecond = pTime->get_second();
            m_nYear = pTime->get_year();
            m_nMonth = pTime->get_month();
            m_nDay = pTime->get_day();
            m_nHour = pTime->get_hour();
        }

        if( 0 == ret )
        {
            mgr::container()->crt()->sprintf( pStr, "%04u-%02u-%02u %02u:%02u:%02u.%03u | %-4s | %02u | %04u | %s | %s",
                m_nYear,
                m_nMonth,
                m_nDay,
                m_nHour,
                m_nMinute,
                m_nSecond,
                m_nMilliSecond,
                m_name.c_str(),
                m_nLevel,
                m_nSrcCodeLine,
                m_file.c_str(),
                m_log.c_str() );
            m_log = pStr->get( 0, 0 );
        }

        return ret;
    }

    int log_msg::init()
    {
        int nRet = 0;
        m_pStrArgsList = mgr::container()->box()->list();
        m_pString = mgr::container()->box()->string();
        m_pTime = mgr::container()->xos()->tm();
        m_pBuf = mgr::container()->box()->buf();
        return nRet;
    }

    int log_msg::term()
    {
        int nRet = 0;
        if( m_fDestroy )
        {
            m_fDestroy( m_pBuf->get_data(0,0,0,0) );
            m_fDestroy = 0;
        }
        xos_stl::release_interface( m_pStrArgsList );
        xos_stl::release_interface( m_pString );
        xos_stl::release_interface( m_pTime );
        xos_stl::release_interface( m_pBuf );
        init_data();
        return nRet;
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////
    // LogMsg method

    // 得到合成后的日志全内容
    const char * log_msg::get_log_content( char * lpszLogContent, int nCharNum, int * lpnRetCharNum )
    {
        int nLen = 0;

        if( ( lpszLogContent || lpnRetCharNum ) )
        {
            nLen = ( int )m_log.length();
        }

        if( lpszLogContent && ( nCharNum > nLen - 1 ) )
        {
            mgr::container()->crt()->strcpy( lpszLogContent, nCharNum, m_log.c_str() );
        }

        if( lpnRetCharNum )
        {
            *lpnRetCharNum = nLen;
        }

        return m_log.c_str();
    }

    // 得到日志时间
    int log_msg::get_log_time( int * lpnYear,
            int * lpnMonth, 
            int * lpnDay,
            int * lpnHour, 
            int * lpnMinute, 
            int * lpnSeconde, 
            int * lpnMilliSecond )
    {
        int nRet = 0;

        if( lpnYear )
        {
            *lpnYear = m_nYear;
        }

        if( lpnMonth )
        {
            *lpnMonth = m_nMonth;
        }

        if( lpnDay )
        {
            *lpnDay = m_nDay;
        }

        if( lpnHour )
        {
            *lpnHour = m_nHour;
        }

        if( lpnMinute )
        {
            *lpnMinute = m_nMinute;
        }

        if( lpnSeconde )
        {
            *lpnSeconde = m_nSecond;
        }

        if( lpnMilliSecond )
        {
            *lpnMilliSecond = m_nMilliSecond;
        }

        return nRet;
    }

    int log_msg::set_log_info( const char * lpszName, int ulLevel, const char * lpszCodeFile, int ulCodeLine, const char * lpszFmt )
    {
        int ret = 0;
        m_nSrcCodeLine = ulCodeLine;
        m_file = lpszCodeFile;
        m_nLevel = ulLevel;
        m_name = lpszName;
        m_pszFmt = lpszFmt;
        m_pTime->get_time();
        return ret;
    }

    int log_msg::set_fmt_log_fun( f_log_fmt_args & fun )
    {
        int ret = 0;
        m_fFmt = fun;
        return ret;
    }

    int log_msg::set_destroy_fun( f_log_destroy & fun )
    {
        int ret = 0;
        m_fDestroy = fun;
        return ret;
    }

    int log_msg::set_fmt( const char * fmt )
    {
        int ret = 0;
        m_pszFmt = fmt;
        return ret;
    }

    const char * log_msg::get_fmt()
    {
        return m_pszFmt;
    }

    xos_box::i_list * log_msg::str_args_list()
    {
        return m_pStrArgsList;
    }

    char * log_msg::args_buf()
    {
        char * pRet = m_pBuf->get_data(0,0,0,0);
        return pRet;
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////
    // LogMsg method

    int log_msg::init_data()
    {
        int nRet = 0;

        m_pStrArgsList = 0;
        m_pBuf = 0;
        m_pString = 0;
        m_pTime = 0;
        m_fDestroy = 0;
        m_fFmt = 0;

        m_pszFmt = 0;
        m_file = "";
        m_name = "";

        m_nSrcCodeLine = 0;
        m_nLevel = 0;
        m_nMilliSecond = 0;
        m_nYear = 0;
        m_nMonth = 0;
        m_nDay = 0;
        m_nHour = 0;
        m_nMinute = 0;
        m_nSecond = 0;

        return nRet;
    }

} // xos_log
